package com.test.list.contacts;

/** Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.test.list.imageLoading.ImageLoader;
import com.test.list.imageLoading.ImageUtils;
import com.test.list.interfaces.Viewable;
import com.test.list.simple.Controller;

public class DeviceContact implements Viewable
{
	private String mId;
	private String mDisplayName;
	// private String _photoId;
	private String mLookUpKey;

	// , String photoId
	public DeviceContact(String id, String name, String lookUpKey)
	{

		setId(id);
		setDisplayName(name);
		// setPhotoId(photoId);
		setLookUpKey(lookUpKey);
	}

	// public String get_photoId()
	// {
	// return _photoId;
	// }

	public String getId()
	{
		return mId;
	}

	// public String getDisplayName()
	// {
	//
	// }

	// public String getLookUpKey()
	// {
	// return _lookUpKey;
	// }

	// public void setPhotoId(String _photoId)
	// {
	// this._photoId = _photoId;
	// }

	public void setId(String id)
	{
		mId = id;
	}

	public void setDisplayName(String displayName)
	{
		mDisplayName = displayName;
	}

	public void setLookUpKey(String lookUpKey)
	{
		mLookUpKey = lookUpKey;
	}

	@Override
	public String getMainHeader()
	{
		return mDisplayName;
	}

	@Override
	public String getSecondaryHeader()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getMainPictureID()
	{
		return mId;
	}

	@Override
	public String getSecondaryPictureID()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getKey()
	{
		return mLookUpKey;
	}

	@Override
	public Bitmap getBitmap()
	{
		Bitmap retVal = null;
		final ContentResolver resolver = Controller.getContext()
				.getContentResolver();
		try
		{
			Uri uri = ContactMngr.getInstance().getContactPhotoUri(
					Long.valueOf(getMainPictureID()));
			InputStream input = resolver.openInputStream(uri);

			BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
			onlyBoundsOptions.inJustDecodeBounds = true;
			onlyBoundsOptions.inDither = true;// optional
			onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
			BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
			input.close();
			if ((onlyBoundsOptions.outWidth == -1)
					|| (onlyBoundsOptions.outHeight == -1))
				return null;

			int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
					: onlyBoundsOptions.outWidth;

			double ratio = (originalSize > ImageLoader.THUMBNAIL_SIZE) ? (originalSize / ImageLoader.THUMBNAIL_SIZE)
					: 1.0;

			BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
			bitmapOptions.inSampleSize = ImageUtils
					.getPowerOfTwoForSampleRatio(ratio);
			bitmapOptions.inDither = true;// optional
			bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
			input = resolver.openInputStream(uri);
			retVal = ImageUtils.getRoundedBitmap(
					BitmapFactory.decodeStream(input, null, bitmapOptions), 12);// ,
																				// android.R.color.transparent);

			input.close();
		} catch (FileNotFoundException fe)
		{

		} catch (IOException ioE)
		{

		}

		return retVal;
	}

	
	/**
	 * 
	 */
	@Override
	public void onClick()
	{
		
	}

	/**
	 * 
	 */
	@Override
	public void onLongClick()
	{
		
	}
}
